#coding:utf-8
from gensim.models import word2vec, Word2Vec
import gensim
import logging
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.INFO)
#第一次使用,需要加载文档集(http://mattmahoney.net/dc/text8.zip)
#sentences = word2vec.Text8Corpus('data/text8')
#model = word2vec.Word2Vec(sentences, size=200)
#保存模型,以便下次直接使用
#model.save('data/text8.model')
#下次使用的时候,无需加载sentences文档集,直接读取model啦
model = Word2Vec.load('data/text8.model')
print model.similarity("dictionary","lexicon")
print model.similarity("cheer","encourage")
print model.similarity("vulnerable","weak")
print model.similarity("grasp","capture")
print model.similarity("wisdom","intelligence")
print model.similarity("admit","acknowledge")
print model.similarity("gain","acquire")
print model.similarity("dim","vague")
print model.similarity("skeptical","suspicious")
print model.similarity("salary","wage")
print model.similarity("conference","meeting")
#word2vec好玩的地方,计算相似词,woman+man+kiss+love-girl=bride
#print model.most_similar(positive=['woman','man','kiss','love'],negative=['girl'],topn=5)
#结果如下[('bride', 0.6755753755569458), ('me', 0.6339389681816101), 
#('baby', 0.6337762475013733), ('lady', 0.6284192204475403), ('devil', 0.6243280172348022)]